package com.cn;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ActivityInstance;
import org.flowable.task.api.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 创建文件方式
 */
@SpringBootTest
public class flowableTest06 {

    @Autowired
    private ProcessEngine processEngine;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RepositoryService repositoryService;


    /**
     * 创建流程引擎的方式
     * ACT REDEPLOYMENT 是否创建成功
     */
    @Test
    void createProcess(){
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process01-基础/学生审批.bpmn20.xml")
                .name("学生审批")
                .deploy();
        System.out.println("deploy.getId()="+deploy.getId());
    }

    /**\
     * 创建任务启动
     */
    @Test
    void startRun(){
        String id = "studeshengpi:1:7d885474-b115-11ee-9e48-30d04231d1ff";
        runtimeService.startProcessInstanceById(id);
    }


    /**
     * 开始讨论
     * 进行换人的方式
     */
    @Test
    void claims(){
        List<Task> list = taskService.createTaskQuery()
                .taskCandidateUser("百里登封")
                .list();
        for (Task task : list) {
            taskService.claim(task.getId(),"百里甄选");
        }
    }

    /**
     * 任务完成代办任务的方式
     */
    @Test
    void startTask(){
//        List<Task> list = taskService.createTaskQuery()
//                .taskAssignee("百里甄选")
//                .list();
//        for (Task task : list) {
//            taskService.complete(task.getId());
//        }
        Map<String,Object > map = new HashMap<>();
        map.put("te","li");
        taskService.complete("e708071c-b0eb-11ee-967c-30d04231d1ff",map);
    }

    /**
     * 根据用户名称查询待办的方式
     */
    @Test
    void findTask(){
        List<Task> list = taskService.createTaskQuery()
                .taskName("经理")
                .list();
        for (Task task : list) {
            System.out.println("task.getId()"+task.getId());
        }
    }
}